home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3863 / 3863.xpi / chrome / imacros.jar / content / TreeView.js < prev    next >
Text File  |  2010-01-25  |  15KB  |  5 lines

  1. //*/
  2. /*
  3. (c) Copyright 2008 iOpus Software GmbH - http://www.iopus.com
  4. */
  5. var theTreeHelperObject=null;function MacrosTree(tree,children_id,cur_wnd){this.tree=tree;this.wnd=cur_wnd;this.children_id=children_id;return this;};MacrosTree.prototype={tree:null,children_id:null,wnd:null,sep:null,get children(){return this.doc.getElementById(this.children_id);},get doc(){return this.wnd.document;},labelFromItem:function(item){var child=item.firstChild;while(child){if(child.tagName=="treerow"){if(child.firstChild.tagName=="treecell"){var label=child.firstChild.getAttribute("label");return label;}}child=child.nextSibling;}return null;},treechildrenFromItem:function(item){var child=item.firstChild;while(child){if(child.tagName=="treechildren"){return child;}child=child.nextSibling;}return null;},getContainer:function(name,parent){var items=parent.childNodes;for(var i=0;i<items.length;i++){if(items.item(i).getAttribute("container")!="true")continue;var item=items.item(i);if(this.labelFromItem(item)==name)return this.treechildrenFromItem(item);}var item=this.doc.createElement("treeitem");item.setAttribute("container","true");var row=this.doc.createElement("treerow");var con=this.doc.createElement("treechildren");item.appendChild(row);item.appendChild(con);var cell=this.doc.createElement("treecell");cell.setAttribute("label",name);row.appendChild(cell);var child=parent.firstChild;while(child){if(child.getAttribute("container")=="true"){var li=this.labelFromItem(item).toLowerCase();var ci=this.labelFromItem(child).toLowerCase();if(li<ci){parent.insertBefore(item,child);break;}}else{parent.insertBefore(item,child);break;}child=child.nextSibling;}if(!child){parent.appendChild(item);}return con;},insertLeaf:function(name,parent){var src,match,properties="";if(match=name.match(/\.(js|iim)$/i)){if(match[1]=="js"){properties="js-file";}else if(match[1]=="iim"){properties="iim-file";}}else{return;}var item=this.doc.createElement("treeitem");var row=this.doc.createElement("treerow");var cell=this.doc.createElement("treecell");cell.setAttribute("properties",properties);cell.setAttribute("label",name);row.appendChild(cell);item.appendChild(row);var child=parent.firstChild;while(child){if(child.getAttribute("container")!="true"){var li=this.labelFromItem(item).toLowerCase();var ci=this.labelFromItem(child).toLowerCase();if(treeItemsCmp(li,ci)<0){parent.insertBefore(item,child);break;}}child=child.nextSibling;}if(!child){parent.appendChild(item);}},initializeTree:function(arr,sep){while(this.children.hasChildNodes()){var nodes=this.children.childNodes;for(var i=0;i<nodes.length;i++){this.children.removeChild(nodes.item(i));}}this.tree.treeBoxObject.invalidate();this.sep=sep;for(var i=0;i<arr.length;i++){var path=arr[i].split(sep),x=0,con=this.children;while(x<path.length-1){con=this.getContainer(path[x],con);x++;}this.insertLeaf(path[x],con);}},findItem:function(name){if(name=="")return this.children.parentNode;var con=this.children;var path=name.split(this.sep);for(var i=0;i<path.length;i++){var item=con.firstChild;while(item){if(this.labelFromItem(item)==path[i]){var tcon=this.treechildrenFromItem(item);if(i==path.length-1){return item;}else if(path[path.length-1]==""&&i==path.length-2){return item;}else if(!tcon){return null;}else{con=tcon;break;}}item=item.nextSibling;}if(!item)return null;}return null;},getItemAt:function(idx){var ret={isContainer:false,DOMNode:null,path:null};if(this.tree.view.isContainer(idx)){ret.isContainer=true;}var cidx=idx;var s="";while(this.tree.view.getLevel(cidx)>0){var item=this.tree.contentView.getItemAtIndex(cidx);if(this.tree.view.isContainer(cidx)){s=this.labelFromItem(item)+this.sep+s;}else{s=this.labelFromItem(item)+s;}cidx=this.tree.view.getParentIndex(cidx);}ret.path=s;ret.DOMNode=this.tree.contentView.getItemAtIndex(idx);return ret;},getIndexOfItem:function(item){return this.tree.contentView.getIndexOfItem(item);},getItemPath:function(item){var idx=this.getIndexOfItem(item);if(idx== -1)return null;var node=this.getItemAt(idx);if(!node)return null;return node.path;},getSelectedItem:function(){var idx=this.tree.currentIndex;if(idx== -1)return null;return this.getItemAt(idx);},insertItem:function(item,path){var con=this.findItem(path);if(!con||con.getAttribute("container")!="true")return null;if(!(con=this.treechildrenFromItem(con)))return null;var child=con.firstChild;while(child){if(item.getAttribute("container")=="true"){if(child.getAttribute("container")=="true"){if(this.labelFromItem(item)<this.labelFromItem(child)){con.insertBefore(item,child);break;}}else{con.insertBefore(item,child);break;}}else{if(child.getAttribute("container")!="true"){var li=this.labelFromItem(item).toLowerCase();var ci=this.labelFromItem(child).toLowerCase();if(treeItemsCmp(li,ci)<0){con.insertBefore(item,child);break;}}}child=child.nextSibling;}if(!child){con.appendChild(item);}return item;},removeItem:function(path){var item=this.findItem(path);if(!item)return null;var con=item.parentNode;return con.removeChild(item);},_treewalker:function(item,arr,cur_path){if(item.getAttribute("container")=="true"){cur_path+=this.labelFromItem(item)+this.sep;var tc=this.treechildrenFromItem(item);if(tc.hasChildNodes()){var child=tc.firstChild;while(child){arr=this._treewalker(child,arr,cur_path);child=child.nextSibling;}}else{arr.push(cur_path);}}else{arr.push(cur_path+this.labelFromItem(item));}return arr;},getItemNamesArray:function(){var arr=new Array();if(this.children.hasChildNodes()){var child=this.children.firstChild;while(child){var cur_path="";arr=this._treewalker(child,arr,cur_path);child=child.nextSibling;}}return arr;},sortItems:function(sort_func){var arr=this.getItemNamesArray();arr.sort(sort_func);this.initializeTree(arr,this.sep);}};function getMTreeObject(){return theTreeHelperObject;};function treeItemsCmp(a,b){var la=a.toLowerCase(),lb=b.toLowerCase();var bound=Math.min(la.length,lb.length);for(var i=0;i<bound;i++){var l=la.charAt(i),r=lb.charAt(i),x;if(l==r)continue;if(l=="#")return-1;else if(r=="#")return 1;else if(x=l.localeCompare(r))return x;}return la.length-lb.length;};function getLastPathNode(path){var arr=path.split(__psep());return arr.pop();};function _readdir(entry,arr,cur_path){if(entry.isDirectory()){var entries=entry.directoryEntries;cur_path+=getLastPathNode(entry.path.toString())+__psep();arr.push(cur_path);while(entries.hasMoreElements()){var en=entries.getNext().QueryInterface(imns.Ci.nsILocalFile);arr=_readdir(en,arr,cur_path);}}else if(entry.isFile()){cur_path+=getLastPathNode(entry.path.toString());arr.push(cur_path);}return arr;};function getMacroEntries(){try{var dir=im_Pref.getFilePref("defsavepath");if(dir&&dir.exists()){var arr=new Array();var entries=dir.directoryEntries;while(entries.hasMoreElements()){var entry=entries.getNext().QueryInterface(imns.Ci.nsILocalFile);arr=_readdir(entry,arr,"");}return arr;}else{var path=im_Pref.getFilePref("defsavepath");if(path)path=path.path;else path=im_Pref.getCharPref("defsavepath",true);Components.utils.reportError("can not open "+path);var msg="Can not open "+path+" path to read macros.\n"+"Would you like to correct path settings now?";if(im_Dialogs.confirm(msg)){window.openDialog('chrome://imacros/content/options.xul');}return null;}}catch(e){Components.utils.reportError(e);}return null;};function onDragStart(event){try{var mtree=getMTreeObject();var idx=mtree.tree.treeBoxObject.getRowAt(event.pageX,event.pageY);if(idx== -1||idx==0)return;var item=mtree.getSelectedItem();if(idx!=mtree.getIndexOfItem(item.DOMNode))return;event.dataTransfer.setData('text/plain',item.path);__loginf("onDragStart, path="+item.path);event.dataTransfer.effectAllowed="move";}catch(e){Components.utils.reportError(e);}};function onDragOver(event){try{var mtree=getMTreeObject();var idx=mtree.tree.treeBoxObject.getRowAt(event.pageX,event.pageY);var canDrop=false;if(idx!= -1){var dstNode=mtree.getItemAt(idx);canDrop=dstNode&&dstNode.isContainer&&event.dataTransfer.types.contains("text/plain");}if(canDrop){event.dropEffect="move";event.preventDefault();}}catch(e){Components.utils.reportError(e);}};function onDragDrop(event){try{if(!event.dataTransfer.types.contains("text/plain"))return;var src_path=event.dataTransfer.getData("text/plain");var mtree=getMTreeObject();var idx=mtree.tree.treeBoxObject.getRowAt(event.pageX,event.pageY);if(idx== -1)return;var dstNode=mtree.getItemAt(idx);if(!dstNode)return;var dst_path,sep=__psep();if(dstNode.isContainer){dst_path=dstNode.path;}else{if(dstNode.path.indexOf(sep)!= -1)dst_path=dstNode.path.split(sep).slice(0,-1).join(sep);else dst_path="";}var path_nodes;try{var src=im_FIO.openMacroFile(src_path);}catch(e){Components.utils.reportError(e);return;}if(!src|| !src.exists())return;var dst=im_FIO.openMacroFile(dst_path);src.moveTo(dst,"");var srcNode=mtree.removeItem(src_path);if(!srcNode)return;mtree.insertItem(srcNode,dst_path);event.preventDefault();}catch(e){Components.utils.reportError(e);}};function setDragHandlers(){var tree=document.getElementById("treeview");tree.addEventListener("dragstart",onDragStart,false);tree.addEventListener("dragenter",onDragOver,false);tree.addEventListener("dragover",onDragOver,false);tree.addEventListener("drop",onDragDrop,false);};function MTSortPredicate(a,b){var a_is_dir=a.indexOf(__psep())!= -1;var b_is_dir=b.indexOf(__psep())!= -1;if(!a_is_dir&& !b_is_dir){return treeItemsCmp(a,b);}else if(a_is_dir&& !b_is_dir){return-1;}else if(!a_is_dir&&b_is_dir){return 1;}else{return treeItemsCmp(a,b);}};function initMTree(){var arr=getMacroEntries();if(!arr){return;}arr.sort(MTSortPredicate);var tree=document.getElementById("treeview");if(!tree)throw "no treeview was found";if(!theTreeHelperObject)theTreeHelperObject=new MacrosTree(tree,"favtree",window);theTreeHelperObject.initializeTree(arr,__psep());};function checkSIActive(){if(!__is_windows())return;var sibox=document.getElementById("imacros-si-info");var sitext=document.getElementById("imacros-si-text");const href="http://www.iopus.com/imacros/home/fx/rd.asp?helpid=";sibox.collapsed=false;sitext.value="About iMacros";sitext.href=href+"siactiveno";try{var wrk=imns.Cc["@mozilla.org/windows-registry-key;1"].createInstance(imns.Ci.nsIWindowsRegKey);wrk.open(wrk.ROOT_KEY_LOCAL_MACHINE,"SOFTWARE",wrk.ACCESS_READ);if(wrk.hasChild("iMacros")){var subkey=wrk.openChild("iMacros",wrk.ACCESS_READ);if(subkey.hasValue("PathBasic")){sitext.value="Scripting Active (Trial)";sitext.href=href+"siactivetr";if(subkey.hasValue("InterfaceType")&&/^fx/.test(subkey.readStringValue("InterfaceType"))){sitext.value="Scripting Active";sitext.href=href+"siactive";}}subkey.close();}wrk.close();try{wrk.create(wrk.ROOT_KEY_CURRENT_USER,"Software\\iMacros",wrk.ACCESS_WRITE);wrk.writeStringValue("fx",imacros_version);wrk.close();}catch(ee){Components.utils.reportError(ee);}}catch(e){Components.utils.reportError(e);}};function MTree_onLoad(event){initMTree();setDragHandlers();checkSIActive();var os=imns.Cc["@mozilla.org/observer-service;1"].getService(imns.Ci.nsIObserverService);os.notifyObservers(window,"imacros-sidebar-loaded",null);};function MTree_onUnLoad(event){var os=imns.Cc["@mozilla.org/observer-service;1"].getService(imns.Ci.nsIObserverService);os.notifyObservers(mainwindow,"imacros-sidebar-closed",null);};window.addEventListener("load",MTree_onLoad,true);window.addEventListener("unload",MTree_onUnLoad,true);function MTree_onSelect(event){if(mainwindow){mainwindow.im_panel.updateSidebarState();}};function MTree_onDblClick(event){if(mainwindow){mainwindow.playunlooped();}};function MPopup_onShowing(event){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item)return;var bookmark=document.getElementById('addbookmark');var editmacro=document.getElementById('editmacro');if(item.isContainer){bookmark.hidden=true;editmacro.hidden=true;}else{bookmark.hidden=false;editmacro.hidden=false;}}catch(e){Components.utils.reportError(e);}};function MPopup_removeItem(){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item)return;var file=im_Pref.getFilePref("defsavepath");var path_nodes=item.path.split(__psep());for(var i=0;i<path_nodes.length;i++)file.append(path_nodes[i]);var strings=imns.Cc["@mozilla.org/intl/stringbundle;1"].getService(imns.Ci.nsIStringBundleService).createBundle("chrome://imacros/locale/rec.properties");if(!confirm(item.isContainer?strings.GetStringFromName("imacrosareyousurewant37"):strings.GetStringFromName("imacrosareyousurewant38")))return;file.remove(true);mtree.removeItem(item.path);}catch(e){Components.utils.reportError(e);}};function MPopup_renameItem(){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item)return;var editbox=document.getElementById('editname');var renamebtn=document.getElementById('im-rename-button');editbox.collapsed=null;editbox.value=mtree.labelFromItem(item.DOMNode);renamebtn.collapsed=null;editbox.focus();editbox.select();}catch(e){Components.utils.reportError(e);}};function MPopup_CreateFolder(){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item)return;var con,idx;if(item.isContainer){con=mtree.treechildrenFromItem(item.DOMNode);idx=mtree.getIndexOfItem(item.DOMNode);if(!mtree.tree.view.isContainerOpen(idx))mtree.tree.view.toggleOpenState(idx);}else{con=item.DOMNode.parentNode;}var basename="New Folder";var name=basename;if(con.hasChildNodes()){var nodes=con.childNodes,found=true,count=1;while(found){found=false;for(var i=0;i<nodes.length;i++){if(mtree.labelFromItem(nodes.item(i))==name){found=true;name=basename+" ("+count+")";count++;break;}}}}var newcon=mtree.getContainer(name,con),t=null;if(!newcon){return;}t=newcon.parentNode;idx=mtree.tree.contentView.getIndexOfItem(t);t=mtree.getItemAt(idx);var path=im_Pref.getFilePref("defsavepath").path+__psep()+t.path;var dir=imns.Cc['@mozilla.org/file/local;1'].createInstance(imns.Ci.nsILocalFile);dir.initWithPath(path);if(!dir.exists())dir.create(0x01,0777);}catch(e){Components.utils.reportError(e);}};function MPopup_AddBookmark(type){mainwindow.addbookmark(type);};function MPopup_EditMacro(){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item||item.isContainer)return;mainwindow.steps();}catch(e){Components.utils.reportError(e);}};function onEditKeypress(evt){const VK_ENTER=imns.Ci.nsIDOMKeyEvent.DOM_VK_ENTER;const VK_RETURN=imns.Ci.nsIDOMKeyEvent.DOM_VK_RETURN;if(evt.keyCode==VK_RETURN||evt.keyCode==VK_ENTER){MTree_renameItem();evt.preventDefault();}};function MTree_renameItem(){try{var mtree=getMTreeObject();var item=mtree.getSelectedItem();if(!item)return;var editbox=document.getElementById('editname');var button=document.getElementById('im-rename-button');var ext=item.path.match(/\.(js|iim)$/);if(ext)ext=ext[1];var newname=editbox.value;if(!newname){editbox.collapsed=true;button.collapsed=true;return;}if(!/\.(js|iim)/i.test(newname)&&ext)newname+="."+ext;var file=im_Pref.getFilePref("defsavepath");var path_nodes=item.path.split(__psep());for(var i=0;i<path_nodes.length;i++)file.append(path_nodes[i]);file.moveTo(null,newname);mtree.tree.view.setCellText(mtree.tree.currentIndex,mtree.tree.columns.getFirstColumn(),newname);editbox.collapsed=true;button.collapsed=true;}catch(e){Components.utils.reportError(e);}}